package com.sso.Login.controller;


import com.sso.Login.pojo.User;
import com.sso.Login.utils.LoginCacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/*
* 页面跳转逻辑
* */
@Controller
@RequestMapping("/view")
public class ViewController {
    @GetMapping("/login")
    public String toLogin(@RequestParam(required = false,defaultValue = "") String target, HttpSession session,@CookieValue( required = false,name = "TOKEN") Cookie cookie){
        if (StringUtils.isEmpty(target)){
            target = "http://localhost:9000";
        }

        //如果用户登录过 cookie中有token
        if (cookie!=null) {
            User user = LoginCacheUtil.loginUser.get(cookie.getValue());
            if (user != null) {
                return "redirect:" + target;
            }
        }

        session.setAttribute("target",target);
        return "login";
    }
}
